{% extends '../layout.html' %}

{% block body %}
<section class="content-header">
    <h1>
        系统设置
        <small></small>
    </h1>
</section>

<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <!-- general form elements -->
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">系统设置</h3>
                </div>
                <!-- form start -->
                <form method="post" action="#"  class="form-horizontal" id="add_form">
                    <div class="box-body">
                        <input class="form-control" name="id" placeholder="" value="{{config._id}}"
                               type="hidden">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">配置标识（用于C函数调用，只能使用英文且不能重复）</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="key" placeholder="" value="{{config.key}}"
                                       type="text">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">配置标题（用于后台显示的配置标题）</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="title" placeholder="" value="{{config.title}}"
                                       type="text">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">排序（用于分组显示的顺序）</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="sort" placeholder="" value="{{config.sort}}"
                                       type="number">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">配置类型（系统会根据不同类型解析配置值））</label>
                            <div class="col-sm-10">
                                <select name="valueType" class="form-control">
                                    {% for item in valueTypes %}
                                    <option value="{{item.key}}" {% if item.key == config.valueType %}selected{% endif %}>{{item.title}}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">配置分组（配置分组 用于批量设置 不分组则不会显示在系统设置中）</label>
                            <div class="col-sm-10">
                            <select name="group" class="form-control">
                                {% for item in groups %}
                                <option value="{{item.key}}" {% if item.key == config.group %}selected{% endif %}>{{item.title}}</option>
                                {% endfor %}
                            </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">配置值（配置值）</label>

                            <div class="col-sm-10">
                                <textarea class="form-control" name="value" rows="3">{{config.value}}</textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">配置项（如果是枚举型 需要配置该项）</label>

                            <div class="col-sm-10">
                                <textarea class="form-control" name="extra" rows="3">{{config.extra}}</textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">说明（配置详细说明）</label>

                            <div class="col-sm-10">
                                <textarea class="form-control" name="remark" rows="3">{{config.remark}}</textarea>
                            </div>
                        </div>

                    </div>
                    <!-- /.box-body -->

                    <div class="box-footer">
                        <div class="col-sm-2">
                            <button type="button" id="btnSave" class="btn btn-block btn-danger">保存</button>
                        </div>
                    </div>
                </form>
            </div>
            <!-- /.box -->

        </div>
        <!--/.col (right) -->
    </div>
</section>

<script>
    $(function () {
        $("#btnSave").click(function () {
            doPost();
        });
    });

    function doPost(){
        const index = layer.load(0, {time: 5 * 1000}); //显示加载中动画，最多5秒自动关闭
        $.ajax({
            type: "post",
            url: `/admin/config/add`,
            data: $("#add_form").serializeArray(),
            beforeSend: function (xhr) {
                xhr.setRequestHeader('x-pjax', 'true');
            },
            success: function (data) {
                if (data.hasOwnProperty('errno')){
                    layer.msg(data.errmsg, {icon: 2});
                }else{

                }

                layer.close(index); //关闭加载动画
            },
            error: function (xhr) {
                layer.close(index); //关闭加载动画
                if (xhr.status === 401) {
                    layer.msg('未授权访问接口！', {icon: 2});
                } else {
                    layer.msg('通讯失败！请重试！', {icon: 2});
                }
            }
        });
    }
</script>
{% endblock %}

